package windowFactoryPackage;

import java.sql.DriverManager;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;

public class Credentials {
	static String pass = "";
	static JPasswordField password = new JPasswordField();
	// set to true if connection failed
	static boolean flagConnection = false;

	public static void setPassword() {
		JOptionPane.showMessageDialog(null, password, "Enter Password: ",
				JOptionPane.INFORMATION_MESSAGE);
		// turn password char[] into String
		char[] passChar = password.getPassword();
		for (int i = 0; i < passChar.length; i++) {
			pass += passChar[i];
		}

		// test password to connect to DB
		try {
			// Step 1: Load the JDBC driver.
			Class.forName("oracle.jdbc.driver.OracleDriver");
			// Step 2: Establish the connection to the database.
			String url = Query.connectionURL;
			DriverManager.getConnection(url, Query.connectionName, pass);

		} catch (Exception ex) {
			System.err.println("Got an exception! Credentials.setPassword()");
			System.err.println(ex.getMessage());
			// caught error (invalid password) try again
			JOptionPane.showMessageDialog(null, "Connection Failed!");
			System.exit(0);
		}
		// caught no errors, successful (unless DB is down)
		JOptionPane.showMessageDialog(null, "Connection Successful!");
		System.out.println("Connection Successful");

	}

}